package cn.arnold.offer.guanglianda;

import java.util.Scanner;

/**
 * @Author Arnold
 * @Desc 元素平衡
 **/
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] s1 = s.split(" ");
        long a = Long.parseLong(s1[0]);
        long b = Long.parseLong(s1[1]);
        long c = Long.parseLong(s1[2]);
        long d = Long.parseLong(s1[3]);
        System.out.println(fun(a, b, c, d));
    }

    public static long fun(long a, long b, long c, long d) {
        long sum = a + b + c + d;
        long avl = sum / 4;
        for (long i = avl; i >= 0; i--) {
            long l = sum - 4 * i;
            long r = 0;
            if (i - a > 0) {
                r += i - a;
            }
            if (i - b > 0) {
                r += i - b;
            }
            if (i - c > 0) {
                r += i - c;
            }
            if (i - d > 0) {
                r += i - d;
            }
            if (l >= r) {
                return 4 * i;
            }
        }
        return -1;
    }
}
